#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.const import ReportDBLabel
from generaldb.gbase8a.common.const import RestoreSubJobName, AllStepEnum, EnvName
from generaldb.gbase8a.job_manager import JobManager
from generaldb.gbase8a.param.parse_backup_params import GbaseParseBackupParamParam
from generaldb.gbase8a.service.base_service import MetaService
from generaldb.gbase8a.service.backup.backup_task import BackupTask
from generaldb.gbase8a.service.restore import prerequisite, restore, gen_sub


class BackupService(MetaService):
    def __init__(self, job_manager: JobManager, param_dict):
        super().__init__(job_manager, param_dict)
        self.param = self.gen_param_obj()
        self.job_manager = job_manager
        EnvName.DB_USER_NAME = "job_protectEnv_auth_authKey"
        EnvName.DB_PASSWORD = "job_protectEnv_auth_authPwd"
        EnvName.CUSTOM_SETTINGS = "job_protectEnv_auth_extendInfo_authCustomParams"
        self.backup_manager = BackupTask(self.job_manager, self.param)

    def gen_param_obj(self):
        param = GbaseParseBackupParamParam(self.param_dict, self.job_manager)
        return param

    def get_steps(self):
        pass


@JobManager.register(AllStepEnum.ALLOW_BACKUP_IN_LOCAL_NODE)
class AllowBackupService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False

    def get_steps(self):
        """
        """
        default_allow = [
            self.backup_manager.backup_allow
        ]
        allow_restore_map = {
            RestoreSubJobName.PREPARE_RESTORE: [],
        }
        sub_job = self.param.get_subjob_jobname()
        steps = allow_restore_map.get(sub_job, default_allow)
        return steps


@JobManager.register(AllStepEnum.PRE_TASK)
class PrerequisiteBackupService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
        self.log_detail.update(prerequisite)

    def get_steps(self):
        """

        """
        steps = [
            # 通过执行show database 验证数据库户名密码
            self.backup_manager.backup_pre_task,
        ]
        return steps


@JobManager.register(AllStepEnum.CHECK_BACKUP_JOB_TYPE)
class CheckBackupType(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
        self.log_detail.update(prerequisite)

    def get_steps(self):
        """
        """
        steps = [
            self.backup_manager.check_backup_job_type,
        ]
        return steps


@JobManager.register(AllStepEnum.BACKUP_GEN_SUB)
class GenSubBackupService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
        self.log_detail.update(gen_sub)

    def get_steps(self):
        steps = [
            self.backup_manager.gen_sub_job
        ]
        return steps


@JobManager.register(AllStepEnum.BACKUP)
class BackupExecuteService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
        self.log_detail.update({
            "failed": ReportDBLabel.BACKUP_SUB_FAILED,
            "start": ReportDBLabel.BACKUP_SUB_START_COPY,
            "success": ReportDBLabel.SUB_JOB_SUCCESS
        })

    def get_steps(self):
        steps = [self.backup_manager.backup]
        return steps
    

@JobManager.register(AllStepEnum.QUERY_SCAN_REPOSITORIES)
class QueryScanRepositoriesService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False

    def get_steps(self):
        steps = [
            self.backup_manager.query_scan_repositories
        ]
        return steps


@JobManager.register(AllStepEnum.POST_TASK)
class PostBackupService(BackupService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True

    def get_steps(self):
        steps = [
            self.backup_manager.backup_post_job
        ]
        return steps


@JobManager.register(AllStepEnum.STOP_TASK)
class AbortJob(BackupService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False

    def get_steps(self):
        steps = [
            self.backup_manager.backup_abort_job
        ]
        return steps
